package comprehensivePractice;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/4/19
 */

import java.util.Arrays;
import java.util.Scanner;

public class Practice03 {
    // 预定义学生列表（最多支持10个学生，可扩展）
    static String[] studentNames = {"张三", "李四", "王五"}; // 学生姓名
    static String[] courseNames = {"oracle", "web"};       // 课程名称
    static double[][] scores = { {97.0, 85.0,99.0},{72.0, 89.0,89.5},{99.0, 88.0,100.0}};    // 成绩：scores[学生索引][课程索引]
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("**********************");
        System.out.println("欢迎进入学生管理系统");
        System.out.println("**********************");
        System.out.println("提示: 你可以使用HELP命令查看帮助");
        while (true) {
            System.out.print("SMS>");
            String input = scanner.nextLine().trim();
            if (input.isEmpty()){
                continue;
            }
            String[] parts = input.split(" ");
            String command = parts[0].toUpperCase();
            String[] params = Arrays.copyOfRange(parts, 1, parts.length);

            switch (command) {
                case "HELP":
                    printHelp();
                    break;
                case "AVGCOU":
                    handleAvgCourse(params, scanner);
                    break;
                case "AVGSTU":
                    handleAvgStudent(params, scanner);
                    break;
                case "SORT":
                    handleSort(params, scanner);
                    break;
                case "GET":
                    handleGetScore(params, scanner);
                    break;
                case "EXIT":
                    System.out.println("退出系统");
                    scanner.close();
                    return;
                default:
                    System.out.println("命令错误，输入HELP查看帮助");
            }
        }
    }

    // 打印帮助信息
    private static void printHelp() {
        System.out.println("支持的命令：");
        System.out.println("1. help 显示帮助");
        System.out.println("2. AVGCOU 课程名 - 计算某课程的平均分");
        System.out.println("3. AVGSTU 学生名 - 计算某学生的平均分");
        System.out.println("4. SORT 课程名 - 按某课程成绩排序");
        System.out.println("5. GET 学生名 课程名 - 查询某学生某课程的成绩");
        System.out.println("6. EXIT - 退出系统");

    }

    // 计算课程平均分
    private static void handleAvgCourse(String[] params, Scanner scanner) {
        if (params.length == 0) {
            System.out.println("请输入课程名，例如：AVGCOU web");
            return;
        }
        String course = params[0];
        int courseIndex = findCourseIndex(course); // 查找课程索引
        if (courseIndex == -1) {
            System.out.println("课程不存在（现有课程：oracle, web）");
            return;
        }

        double total = 0;
        int count = 0;
        for (int i = 0; i < studentNames.length; i++) {
            total += scores[i][courseIndex];
            count++;
        }
        double average = total / count;
        System.out.println(course + "课程的平均分是：" + average);
    }

    // 计算学生平均分
    private static void handleAvgStudent(String[] params, Scanner scanner) {
        if (params.length == 0) {
            System.out.println("请输入学生名，例如：AVGSTU 张三");
            return;
        }
        String student = params[0];
        int studentIndex = findStudentIndex(student); // 查找学生索引
        if (studentIndex == -1) {
            System.out.println("学生不存在（现有学生：张三, 李四, 王五）");
            return;
        }

        double total = 0;
        for (int i = 0; i < courseNames.length; i++) {
            total += scores[studentIndex][i];
        }
        double average = total / courseNames.length;
        System.out.println(student + "的平均分是：" + average);
    }

    // 按课程成绩排序
    private static void handleSort(String[] params, Scanner scanner) {
        if (params.length == 0) {
            System.out.println("请输入课程名，例如：SORT oracle");
            return;
        }
        String course = params[0];
        int courseIndex = findCourseIndex(course);
        if (courseIndex == -1) {
            System.out.println("课程不存在");
            return;
        }

        // 复制成绩数组并排序（冒泡排序）
        double[] tempScores = new double[studentNames.length];
        for (int i = 0; i < studentNames.length; i++) {
            tempScores[i] = scores[i][courseIndex];
        }

        // 冒泡排序
        for (int i = 0; i < tempScores.length - 1; i++) {
            for (int j = 0; j < tempScores.length - i - 1; j++) {
                if (tempScores[j] > tempScores[j + 1]) {
                    double temp = tempScores[j];
                    tempScores[j] = tempScores[j + 1];
                    tempScores[j + 1] = temp;
                }
            }
        }

        System.out.println(course + "课程成绩排序：" + Arrays.toString(tempScores));
    }

    // 查询单科成绩
    private static void handleGetScore(String[] params, Scanner scanner) {
        if (params.length < 2) {
            System.out.println("请输入学生名和课程名，例如：GET 张三 web");
            return;
        }
        String student = params[0];
        String course = params[1];
        int studentIndex = findStudentIndex(student);
        int courseIndex = findCourseIndex(course);

        if (studentIndex == -1) {
            System.out.println("学生不存在");
            return;
        }
        if (courseIndex == -1) {
            System.out.println("课程不存在");
            return;
        }

        System.out.println(student + "的" + course + "成绩是：" + scores[studentIndex][courseIndex]);
    }

    // 查找学生索引
    private static int findStudentIndex(String name) {
        for (int i = 0; i < studentNames.length; i++) {
            if (studentNames[i].equals(name)) {
                return i;
            }
        }
        return -1; // 未找到
    }

    // 查找课程索引
    private static int findCourseIndex(String name) {
        for (int i = 0; i < courseNames.length; i++) {
            if (courseNames[i].equals(name)) {
                return i;
            }
        }
        return -1; // 未找到
    }
}